﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FoneBuddy.LinqModel.Data;
using FoneBuddy.LinqModel;
using FoneBuddy.LinqData;

public partial class City_List : System.Web.UI.Page
{
    IDataRepository _dataObject = new DataRepository();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            getCity();

        }
    }

    #region BindData
    private void getCity()
    {
        City city;
        List<City> cities = new List<City>();
        city = new City(_dataObject);

        cities = city.GetAllCity();

        if (cities != null)
        {
            rptCity.DataSource = cities;
            rptCity.DataBind();
        }
    }
    #endregion 

    #region webmethod for edit and delete
    [System.Web.Services.WebMethod]
    public static string Update(string name, string id, string stateid)
    {
        City city = new City();
        IDataRepository _dataObject = new DataRepository();
        city = new City(_dataObject);
        city.StateId = Convert.ToInt32(stateid);
        city.CountryId = (Int32)EnumClass.Enums.Country.India;
        city.Name = name;
        if (id == "")
            city.Id = 0;
        else
            city.Id = Convert.ToInt32(id);

        city.GetCityByName();
        if (id != "")
        {
            if (city.Id != Convert.ToInt32(id) && city.Id != 0)
            {
                return "0";
            }
            else
            {
                city.IsDeleted = false;
                city.Update();
                return city.Id.ToString();
            }
        }
        else
        {
            if (city.Id > 0)
            {
                return "0";
            }
            else
            {
                city.CreatedDate = System.DateTime.Now;
                city.Create();
                return city.Id.ToString();
            }
        }
    }
    [System.Web.Services.WebMethod]
    public static string Delete(string id)
    {
        try
        {

            FoneBuddy.LinqModel.City filter = new City();
            IDataRepository _dataObject = new DataRepository();
            filter = new City(_dataObject);
            filter.Id = Convert.ToInt32(id);
            filter.Delete();
            return "Deleted";
        }
        catch (Exception ex)
        {
            return "error";
        }

    }
    #endregion

    #region Bind statemaster
    [System.Web.Services.WebMethod]
    public static List<StateInfo> GetState()
    {
        IDataRepository _dataObject = new DataRepository();
        State state = new State();
        List<State> states = new List<State>();
        state = new State(_dataObject);
        states = state.GetAllState();
        // return filters;
        List<StateInfo> ci = new List<StateInfo>();
        for (int i = 0; i < states.Count(); i++)
        {
            ci.Add(new StateInfo()
            {
                Id = Convert.ToInt32(states.ElementAt(i).Id),
                Name = states.ElementAt(i).Name
            });
        }

        return ci;
    }
    public class StateInfo
    {
        public Int32 Id { get; set; }
        public string Name { get; set; }
    }
    #endregion
}